* Split Mysql/Sqlite updates into their own files
[lhc/web/wiklou.git] / includes / installer / DatabaseUpdater.php
1 <?php
2
3 /*
4 * Class for handling database updates. Roughly based off of updaters.inc, with
5 * a few improvements :)
6 */
7 abstract class DatabaseUpdater {
8
9 /**
10 * Array of updates to perform on the database
11 *
12 * @var array
13 */
14 protected $updates = array();
15
16 protected $db;
17
18 protected function __construct( $db ) {
19 $this->db = $db;
20 }
21
22 public static function newForDB( $db ) {
23 switch( $db->getType() ) {
24 case 'mysql':
25 return new MysqlUpdater( $db );
26 case 'sqlite':
27 return new SqliteUpdater( $db );
28 case 'oracle':
29 return new OracleUpdater( $db );
30 default:
31 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
32 }
33 }
34
35 public function doUpdates() {
36 global $IP;
37 require_once( "$IP/maintenance/updaters.inc" );
38 $this->loadUpdates();
39 foreach ( $this->updates as $version => $updates ) {
40 foreach( $updates as $params ) {
41 $func = array_shift( $params );
42 call_user_func_array( $func, $params );
43 flush();
44 }
45 // some updates don't get recorded :(
46 if( $version !== 'always' ) {
47 $this->setAppliedUpdates( $version, $updates );
48 }
49 }
50 }
51
52 protected function loadUpdates() {
53 // If the updatelog table hasn't been upgraded, we can't use the new
54 // style of recording our steps. Run all to be safe
55 if( !$this->canUseNewUpdatelog() ) {
56 $this->updates = $this->getCoreUpdateList();
57 } else {
58 foreach( $this->getCoreUpdateList() as $version => $updates ) {
59 $appliedUpdates = $this->getAppliedUpdates( $version );
60 if( !$appliedUpdates || $appliedUpdates != $updates ) {
61 $this->updates[ $version ] = $updates;
62 }
63 }
64 }
65 $this->getOldGlobalUpdates();
66 }
67
68 protected function getAppliedUpdates( $version ) {
69 $key = "updatelist-$version";
70 $val = $this->db->selectField( 'updatelog', 'ul_value',
71 array( 'ul_key' => $key ), __METHOD__ );
72 if( !$val ) {
73 return null;
74 } else {
75 return unserialize( $val );
76 }
77 }
78
79 protected function setAppliedUpdates( $version, $updates = array() ) {
80 if( !$this->canUseNewUpdatelog() ) {
81 return;
82 }
83 $key = "updatelist-$version";
84 $this->db->delete( 'updatelog', array( 'ul_key' => $key ), __METHOD__ );
85 $this->db->insert( 'updatelog',
86 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
87 __METHOD__ );
88 }
89
90 /**
91 * Updatelog was changed in 1.17 to have a ul_value column so we can record
92 * more information about what kind of updates we've done (that's what this
93 * class does). Pre-1.17 wikis won't have this column, and really old wikis
94 * might not even have updatelog at all
95 *
96 * @return boolean
97 */
98 protected function canUseNewUpdatelog() {
99 return $this->db->tableExists( 'updatelog' ) &&
100 $this->db->fieldExists( 'updatelog', 'ul_value' );
101 }
102
103 /**
104 * Before 1.17, we used to handle updates via stuff like $wgUpdates,
105 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
106 * of this in 1.17 but we want to remain back-compatible for awhile. So
107 * load up these old global-based things into our update list. We can't
108 * version these like we do with our core updates, so they have to go
109 * in 'always'
110 */
111 private function getOldGlobalUpdates() {
112 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
113 $wgExtModifiedFields, $wgExtNewIndexes;
114
115 if( isset( $wgUpdates[ $this->db->getType() ] ) ) {
116 foreach( $wgUpdates[ $this->db->getType() ] as $upd ) {
117 $this->updates['always'][] = $upd;
118 }
119 }
120
121 foreach ( $wgExtNewTables as $tableRecord ) {
122 $this->updates['always'][] = array(
123 'add_table', $tableRecord[0], $tableRecord[1], true
124 );
125 }
126
127 foreach ( $wgExtNewFields as $fieldRecord ) {
128 if ( $fieldRecord[0] != 'user' || $doUser ) {
129 $this->updates['always'][] = array(
130 'add_field', $fieldRecord[0], $fieldRecord[1],
131 $fieldRecord[2], true
132 );
133 }
134 }
135
136 foreach ( $wgExtNewIndexes as $fieldRecord ) {
137 $this->updates['always'][] = array(
138 'add_index', $fieldRecord[0], $fieldRecord[1],
139 $fieldRecord[2], true
140 );
141 }
142
143 foreach ( $wgExtModifiedFields as $fieldRecord ) {
144 $this->updates['always'][] = array(
145 'modify_field', $fieldRecord[0], $fieldRecord[1],
146 $fieldRecord[2], true
147 );
148 }
149 }
150
151 /**
152 * Get an array of updates to perform on the database. Should return a
153 * mutli-dimensional array. The main key is the MediaWiki version (1.12,
154 * 1.13...) with the values being arrays of updates, identical to how
155 * updaters.inc did it (for now)
156 *
157 * @return Array
158 */
159 protected abstract function getCoreUpdateList();
160 }
161
162 class OracleUpdater extends DatabaseUpdater {
163 protected function getCoreUpdateList() {
164 return array();
165 }
166 }